import {defineStore} from 'pinia'

type UserState = {
  _token: string | undefined
  _userInfo: object | undefined
}

export default defineStore('user', {
  state: (): UserState => ({
    _token: undefined,
    _userInfo: undefined
  }),
  getters: {
    token(s): string | undefined {
      return s._token
    },
    userInfo(s): object | undefined {
      return s._userInfo
    }
  },
  actions: {
    setToken(token: string | undefined) {
      this._token = token
    },
    setUserInfo(userInfo: object | undefined) {
      this._userInfo = userInfo
    },
    resetState(){
      this._token = undefined
      this._userInfo = undefined
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'user',
        storage: window.localStorage,
      }
    ]
  }
})
